﻿@using ETD.Common.Extension;
@using ETD.WebOrganization.Libs.Helper;
@using ETD.WinClient.Config;
@using ETD.Domain.EnumModels;
@using ETD.Domain.WebModels;
@using ETD.Common;
@using ETD.WebOrganization.Libs.Controls;
@model ETD.WebOrganization.Areas.Web.Models.FacilityEditModel
@{
    ViewBag.Title = "设备管理";
}
@{

}
<style>
    .text-left {
        text-align: left;
    }

    .weui-btn_primary {
        background-color: #4dd1c1;
        width: 80% !important;
    }

    .securityCheckItem-name {
        font-size: 16px !important;
        line-height: 2 !important;
    }

    .weui-form-preview:after {
        border-bottom: 0px solid #e5e5e5 !important;
    }

    .weui-form-preview {
        margin-top: 32px;
    }

    .weui-picker-container, .weui-picker-overlay {
        position: fixed;
        left: 0;
        right: 0;
        width: 100%;
        z-index: 1000;
    }

    .weui-picker-container {
        background-color: rgba(0, 0, 0, .4);
        height: 100%;
    }

    #btnNbDeviceSaoyisao {
        width: 50px;
        height: 50px;
        display: table-cell;
        text-align: center;
        vertical-align: middle;
    }

    .NbDevice_icon_saoyisao {
        width: 30px;
        height: 30px;
        margin-top: 5px;
    }

    .NbDeviceOut {
        padding-top: 0px;
        padding-bottom: 0px;
    }

    .weui-form-preview__ft {
        border-bottom: 1px solid #d5d5d687;
    }

    .btnActions {
        margin-top: 15px;
    }
</style>
@Html.Hidden("txtFacilityID", Model.FacilityID)
<div class="weui-form-preview">

    <div class="weui-cells__title">设备代码</div>
    <div class="weui-cells">
        <div class="weui-cell NbDeviceOut">
            <div class="weui-cell__bd">
                <input class="weui-input" id="txtCode" name="txtCode" type="text" value="" placeholder="请输入设备代码" maxlength="100">

            </div>
            <div class="weui-cell__ft">
                <a id="btnNbDeviceSaoyisao">
                    <img class="NbDevice_icon_saoyisao" src="@Url.MapMvcAssets("images/icon_saoyisao.png")">
                </a>
            </div>
        </div>
    </div>
    <div class="weui-cells__title">医院</div>
    <div class="weui-cells">
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <input id="txtOrganizationID" type="hidden" value="">
                <input class="weui-input" id="ddlOrganizationList" name="ddlOrganizationList" type="text" value="" placeholder="请选择医院">
            </div>
        </div>
    </div>
    <div class="weui-cells__title">科室</div>
    <div class="weui-cells">
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <input id="txtSectionOfficeID" type="hidden" value="">
                <input class="weui-input" id="ddlSectionOfficeList" name="ddlSectionOfficeList" type="text" value="" placeholder="请选择科室">
            </div>
        </div>
    </div>
    <div class="weui-cells__title">床位号</div>
    <div class="weui-cells">
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <input class="weui-input" id="txtBedNumber" name="txtBedNumber" type="text" value="" placeholder="请输入床位号" maxlength="100">
            </div>
        </div>
    </div>

    <div class="weui-flex btnActions">
        @if (Model.FacilityID > 0)
        {
            <div class="weui-flex__item">
                <div class="placeholder">
                    <button id="btnFeedback" type="submit" class="weui-btn weui-btn_primary" style="color:#f60;" href="javascript:">报修</button>
                </div>
            </div>
        }
        <div class="weui-flex__item">
            <div class="placeholder">
                <button id="btnEdit" type="submit" class="weui-btn weui-btn_primary" href="javascript:"
                        style="margin-top:0px;">
                    修改
                </button>
                <button id="btnSubmit" type="submit" class="weui-btn weui-btn_primary" href="javascript:"
                        style="margin-top:0px;">
                    保存
                </button>
            </div>
        </div>
    </div>
</div>

@section ScriptSection{
    <script>
        $(function () {
            PageObj.init();
        });
        var PageObj = {};
        PageObj.init = function () {
            //InputHelper.numInput("txtBedNumber");

            $("#btnNbDeviceSaoyisao").click(PageObj.onBtnNbDeviceSaoyisaoClick);
            $("#btnSubmit").click(PageObj.onBtnSubmitClick);
            $("#btnEdit").click(PageObj.onBtnEditClick);
            $("#btnFeedback").click(PageObj.onBtnFeedbackClick);


            PageObj.bindForm();
            //edit
            if ($("#txtFacilityID").val()) {
                $("#btnEdit").show();
                $("#btnSubmit").hide();
            } else {//new
                $("#btnEdit").hide();
                $("#btnSubmit").show();
            }

        }
        PageObj.bindForm = function () {
            var paras = {};
            paras.FacilityID = WinHelper.getUrlParam("facilityID");
            if (paras.FacilityID > 0) {
                PageObj.disabledAll();
                ajaxPost("@Url.Action("FindFacility", "Facility")", paras, function (data) {
                    console.log(data);
                    if (data.Status) {
                        var facility = data.Data.Facility;
                        $("#txtCode").val(facility.Code);
                        $("#txtBedNumber").val(facility.BedNumber);
                        var organizationDropdownList = data.Data.OrganizationDropdownList;
                        var sectionOfficeDropdownList = data.Data.SectionOfficeDropdownList;
                        PageObj.bindDdlOrganizationList(data.Data.OrganizationDropdownList, facility.OrganizationID);
                        PageObj.bindDdlSectionOfficeList(data.Data.SectionOfficeDropdownList, facility.SectionOfficeID);
                    } else {
                        $.toast(data.Info, "forbidden");
                    }
                });
            } else {
                PageObj.enabledAll();
                PageObj.findDdlOrganizationList();
                PageObj.findDdlSectionOfficeList();
            }

        }
        PageObj.onBtnFeedbackClick = function () {
            //Feedback/FeedbackEdit?timestamp=20190410175448609&feedbackID=
            WinHelper.redirect("@Url.Action("FeedbackEdit", "Feedback")?FacilityID=@Model.FacilityID");
        }
        PageObj.onBtnEditClick = function () {
            PageObj.enabledAll();
            $("#btnEdit").hide();
            $("#btnSubmit").show();
            $.toptip("可以修改内容了", "warning");
        }
        PageObj.onBtnEditClick = function () {
            PageObj.enabledAll();
            $("#btnEdit").hide();
            $("#btnSubmit").show();
            $.toptip("可以修改内容了", "warning");
        }
        PageObj.disabledAll = function () {
            $('#txtCode').attr("disabled", true);
            $('#btnNbDeviceSaoyisao').attr("disabled", true);
            $('#ddlOrganizationList').attr("disabled", true);
            $('#ddlSectionOfficeList').attr("disabled", true);
            $('#txtBedNumber').attr("disabled", true);
        }
        PageObj.enabledAll = function () {
            $('#txtCode').attr("disabled", false);
            $('#btnNbDeviceSaoyisao').attr("disabled", false);
            $('#ddlOrganizationList').attr("disabled", false);
            $('#ddlSectionOfficeList').attr("disabled", false);
            $('#txtBedNumber').attr("disabled", false);
        }
        PageObj.bindDeviceID = function (nbDeviceID) {
            $("#txtCode").val(nbDeviceID);
        }

        PageObj.findDdlOrganizationList = function () {
            var paras = {};
            ajaxPost("@Url.Action("FindOrganizationDropdownList", "Facility")", paras, function (data) {
                console.log(data);
                if (data.Status) {
                    PageObj.bindDdlOrganizationList(data.Data, null);
                } else {
                    $.toast(data.Info, "forbidden");
                }
            });
        }
        PageObj.bindDdlOrganizationList = function (data, selectedID) {
            var ddlItems = [];
            ddlItems.push({ "title": "--请选择医院--", "value": "" });
            var selectedItem = null;
            if (data) {

                $.each(data, function (index, value) {
                    //{title: "iPhone 8",value: "001",}
                    if (selectedID == value.ID) {
                        selectedItem = value;
                    }
                    ddlItems.push({ "title": value.Name, "value": value.ID });
                });
            }
            if (selectedItem) {
                $("#txtOrganizationID").val(selectedItem.ID);
                $("#ddlOrganizationList").val(selectedItem.Name);
            } else {
                $("#txtOrganizationID").val("");
                $("#ddlOrganizationList").val("");
            }

            $("#ddlOrganizationList").select("update", {
                title: "医院",
                items: ddlItems,
                onChange: function (d) {
                    var changedID = d.values;
                    console.log("ddlOrganizationList changedID=" + changedID + " txtOrganizationID=" + $("#txtOrganizationID").val());
                    if (changedID != $("#txtOrganizationID").val())
                        PageObj.findDdlSectionOfficeList(changedID);

                    $("#txtOrganizationID").val(changedID);

                },
                onClose: function () {
                    console.log("close");
                },
                onOpen: function () {
                    console.log("open");
                },
            });
        }
        PageObj.findDdlSectionOfficeList = function (organizationID) {
            var paras = {};
            paras.OrganizationID = organizationID;
            if (organizationID > 0) {
                ajaxPost("@Url.Action("FindSectionOfficeDropdownList", "Facility")", paras, function (data) {
                    console.log(data);
                    if (data.Status) {
                        PageObj.bindDdlSectionOfficeList(data.Data, null);
                    } else {
                        $.toast(data.Info, "forbidden");
                    }
                });
            } else {
                PageObj.bindDdlSectionOfficeList(null, null);
            }
        }
        PageObj.bindDdlSectionOfficeList = function (data, selectedID) {
            console.log("bindDdlSectionOfficeList start");
            var ddlItems = [];
            ddlItems.push({ "title": "--请选择科室--", "value": "" });
            var selectedItem = null;
            if (data) {
                $.each(data, function (index, value) {
                    //{title: "iPhone 8",value: "001",}
                    if (selectedID == value.ID) {
                        selectedItem = value;
                    }
                    ddlItems.push({ "title": value.Name, "value": value.ID });
                });
            }
            if (selectedItem) {
                $("#txtSectionOfficeID").val(selectedItem.ID);
                $("#ddlSectionOfficeList").val(selectedItem.Name);
            }
            else {
                $("#txtSectionOfficeID").val("");
                $("#ddlSectionOfficeList").val("");
            }
            console.log("bindDdlSectionOfficeList ddlItems=");
            console.log(ddlItems);

            $("#ddlSectionOfficeList").select("update", {
                title: "科室",
                items: ddlItems,
                onChange: function (d) {
                    var changedID = d.values;
                    console.log("ddlSectionOfficeList changedID=" + changedID + " txtSectionOfficeID=" + $("#txtSectionOfficeID").val());
                    //if (changedID != $("#txtSectionOfficeID").val())
                    //    PageObj.findDdlSectionOfficeList(changedID);

                    $("#txtSectionOfficeID").val(changedID);
                },
                onClose: function () {
                    console.log("close");
                },
                onOpen: function () {
                    console.log("open");
                },
            });

        }
        PageObj.onBtnSubmitClick = function () {
            var checkFormResult = PageObj.checkForm();
            if (!checkFormResult) return;
            PageObj.submit();
        }
        PageObj.checkForm = function () {
            var nbDeviceID = $("#txtCode").val();
            if (StringHelper.isNullOrEmpty(nbDeviceID)) {
                $.toptip('请输入设备代码', 'warning');
                return false;
            }
            var organizationID = $("#txtOrganizationID").val();
            if (!StringHelper.isVaildEntityID(organizationID)) {
                $.toptip('请选择医院', 'warning');
                return false;
            }
            var sectionOfficeID = $("#txtSectionOfficeID").val();
            if (!StringHelper.isVaildEntityID(sectionOfficeID)) {
                $.toptip('请选择科室', 'warning');
                return false;
            }
            var bedNumber = $("#txtBedNumber").val();
            if (StringHelper.isNullOrEmpty(bedNumber)) {
                $.toptip('请输入床位号', 'warning');
                return false;
            }


            return true;
        }
        PageObj.submit = function () {
            var paras = {};
            paras.FacilityID = $("#txtFacilityID").val();
            paras.Code = $("#txtCode").val().trim();
            paras.OrganizationID = $("#txtOrganizationID").val().trim();
            paras.SectionOfficeID = $("#txtSectionOfficeID").val().trim();
            paras.BedNumber = $("#txtBedNumber").val().trim();
            console.log(JSON.stringify(paras));

            ajaxPost("@Url.Action("FacilityEdit", "Facility")", paras, function (data) {
                console.log(data);
                if (data.Status) {
                    $.toast(data.Info);
                    window.setTimeout(function () { window.location.href = "@Url.Action("FacilityManage", "Facility")"; }, 2000);
                } else {
                    $.toptip(data.Info, 'warning');
                }
            });
        }
        PageObj.onBtnNbDeviceSaoyisaoClick = function () {
            if (WinHelper.isWeiXin()) {
                wx.scanQRCode({
                    needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                    scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                    success: function (res) {
                        var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                        console.log("扫码结果 result=" + result);
                        //alert("扫码结果 result=" + result);
                        //result = "https://sharebedconsumer.fancylabx.com/Qrcode/Jump?deviceId=d4b83b4f-547f-424f-8667-24d60432fba6";
                        //result = "https://sharebedconsumer.fancylabx.com/Qrcode/Jump?id=1548695286153";

                        var id = WinHelper.getUrlParamByUrl(result, "id");
                        console.log("扫码结果 id=" + id);
                        if (StringHelper.isNullOrEmpty(id)) {
                            $.toptip("扫码失败，无效的二维码", 'warning');
                            return;
                        }
                        PageObj.checkDeviceIDExist(id);
                    }
                });
            }
            else {
                $.toptip('请使用微信打开系统，才能使用扫一扫功能', 'warning');
            }
        }
        PageObj.checkDeviceIDExist = function (id) {
            var paras = {};
            paras.Code = id;
            ajaxPost("@Url.Action("CheckFacilityExist", "Facility")", paras, function (data) {
                console.log(data);
                if (data.Status) {
                    var model = data.Data;
                    if (!model.IsExist) {
                        $.toast("设备不存在", "forbidden");
                        return;
                    }
                    if (model.IsUsed) {
                        $.toast("设备已经被占用", "forbidden");
                        return;
                    }
                    PageObj.bindDeviceID(model.Facility.Code);
                } else {
                    $.toptip(data.Info, 'warning');
                }
            });
        }
    </script>
}